package com.witframework.dynoengine.tool.jspc;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Random;
import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Stdlib {
	private static final int HOW_LONG = 6;
	private static Object SessionIdLock = new Object();

	private String getId() {
		String str = "";

		synchronized (SessionIdLock) {
			long l = System.currentTimeMillis();
			Random localRandom = new Random();
			str = String.valueOf(l);

			for (int i = 1; i <= 6; ++i) {
				str = str + (int) (1.0D + 6.0D * localRandom.nextDouble());
			}
		}
		return str;
	}

	public Stdlib() {
		
	}

	public String ID(Object paramObject1, Object paramObject2,
			Object paramObject3) {
		return getId();
	}

	public String IP(Object paramObject1, Object paramObject2,
			Object paramObject3) {
		HttpServletRequest localHttpServletRequest = (HttpServletRequest) paramObject1;

		return localHttpServletRequest.getRemoteAddr();
	}

	public String HTML(Object paramObject1, Object paramObject2,
			Object paramObject3, String paramString) {
		HttpServletRequest localHttpServletRequest = (HttpServletRequest) paramObject1;
		String str = localHttpServletRequest.getParameter(paramString);

		if (str == null)
			str = "";
		return str;
	}

	public String QUERY(Object paramObject1, Object paramObject2,
			Object paramObject3, String paramString) {
		HttpServletRequest localHttpServletRequest = (HttpServletRequest) paramObject1;
		String str = localHttpServletRequest.getQueryString();

		if (str == null)
			return "";
		return getFromQuery(str, paramString + "=");
	}

	public String QUERY(Object paramObject1, Object paramObject2,
			Object paramObject3) {
		HttpServletRequest localHttpServletRequest = (HttpServletRequest) paramObject1;
		String str = localHttpServletRequest.getQueryString();

		if (str == null)
			return "";
		return str;
	}

	public String COOKIE(Object paramObject1, Object paramObject2,
			Object paramObject3, String paramString) {
		String str = "null";
		int i = 0;
		HttpServletRequest localHttpServletRequest = (HttpServletRequest) paramObject1;

		Cookie[] arrayOfCookie = localHttpServletRequest.getCookies();

		if (arrayOfCookie == null)
			return str;
		try {
			while (i >= 0) {
				if (arrayOfCookie[i].getName().compareTo(paramString) == 0) {
					return arrayOfCookie[i].getValue();
				}
				++i;
			}
		} catch (Exception localException) {
			return str;
		}

		return str;
	}

	public String write(Object paramObject1, Object paramObject2,
			Object paramObject3, String paramString) {
		return paramString;
	}

	public String WRITE(Object paramObject1, Object paramObject2,
			Object paramObject3, String paramString) {
		return write(paramObject1, paramObject2, paramObject3, paramString);
	}

	public String RANDOM(Object paramObject1, Object paramObject2,
			Object paramObject3, int paramInt1, int paramInt2) {
		Random localRandom = new Random();
		return String.valueOf((int) (paramInt1 + paramInt2
				* localRandom.nextDouble()));
	}

	public String println(Object paramObject1, Object paramObject2,
			Object paramObject3, String paramString) {
		return write(paramObject1, paramObject2, paramObject3, paramString);
	}

	public String PRINTLN(Object paramObject1, Object paramObject2,
			Object paramObject3, String paramString) {
		return println(paramObject1, paramObject2, paramObject3, paramString);
	}

	public boolean isSession(Object paramObject1, Object paramObject2,
			Object paramObject3) {
		HttpServletRequest localHttpServletRequest = (HttpServletRequest) paramObject1;
		HttpSession localHttpSession = localHttpServletRequest
				.getSession(false);

		if (localHttpSession == null)
			return false;
		try {
			localHttpSession.getCreationTime();
		} catch (Exception localException) {
			return false;
		}
		return true;
	}

	public void closeSession(Object paramObject1, Object paramObject2,
			Object paramObject3) {
		HttpServletRequest localHttpServletRequest = (HttpServletRequest) paramObject1;
		HttpSession localHttpSession = localHttpServletRequest
				.getSession(false);

		if (localHttpSession == null)
			return;
		try {
			localHttpSession.invalidate();

			return;
		} catch (Exception localException) {
		}
	}

	public void REDIRECT(Object paramObject1, Object paramObject2,
			Object paramObject3, String paramString) {
		PrintWriter localPrintWriter = (PrintWriter) paramObject3;
		try {
			localPrintWriter.println("<br><script>");
			localPrintWriter.println("document.location.href=\"" + paramString
					+ "\"");
			localPrintWriter.println("</script>");
			localPrintWriter.close();

			return;
		} catch (Exception localException) {
		}
	}

	public void redirect(Object paramObject1, Object paramObject2,
			Object paramObject3, String paramString) {
		REDIRECT(paramObject1, paramObject2, paramObject3, paramString);
	}

	public String servlet(Object paramObject1, Object paramObject2,
			Object paramObject3, String paramString) {
		HttpServletRequest localHttpServletRequest = (HttpServletRequest) paramObject1;
		HttpServletResponse localHttpServletResponse = (HttpServletResponse) paramObject2;
		PrintWriter localPrintWriter = (PrintWriter) paramObject3;
		int i = paramString.lastIndexOf(".");

		if ((i > 0) && (i < paramString.length() - 1)) {
			String str1 = paramString.substring(0, i).trim();
			String str2 = paramString.substring(i + 1).trim();
			Object localObject1;
			try {
				Class localClass = Class.forName(str1);

				Object localObject2 = localClass.newInstance();

				Class[] arrayOfClass = { HttpServletRequest.class,
						HttpServletResponse.class, PrintWriter.class };
				Object[] arrayOfObject = { localHttpServletRequest,
						localHttpServletResponse, localPrintWriter };
				Method localMethod = localClass.getMethod(str2, arrayOfClass);

				localObject1 = localMethod.invoke(localObject2, arrayOfObject);
				if (localObject1 == null)
					return "";
			} catch (Exception localException) {
				return "";
			}
			return localObject1.toString();
		}

		return "";
	}

	private String getFromQuery(String paramString1, String paramString2) {
		if (paramString1 == null)
			return "";

		int i = paramString1.indexOf(paramString2);

		if (i < 0)
			return "";
		String str = paramString1.substring(i + paramString2.length());
		i = str.indexOf("&");
		if (i < 0)
			return str;
		return str.substring(0, i);
	}
}
